<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 400px;
            border: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }
        .box input{
            width: 370px ;
            height: 30px;
            border: 0;
            outline: none;
        }
        .box img {
            top: 2px;
            position: absolute;
            right: 20px;
            width: 30px;
        }
    </style>
</head>
<body>
    <div class="box">
        <label for="">
            <img src="./闭眼.png" alt="" id="eye">
        </label>
        <input type="password" name="" id="pwd">
    </div>
    <script>
        let eye = document.getElementById('eye')//获取元素
        let pwd = document.getElementById('pwd')
        //注册事件，处理程序 使用flag变量重新赋值
        let flag = 0;
        eye.onclick = function(){

            //点击一次之后，flag一定要变化
            if(flag == 0){
                pwd.type = 'Text';
                eye.src = './睁眼.png'
                flag = 1;//赋值操作
            }else {
                pwd.type = 'password';
                eye.src = './闭眼.png'
                flag = 0;
            }

        }
    </script>
</body>
</html>